package com.mark.web.data.controller.portal;

import com.mark.common.CurrentUser;
import com.mark.common.ServerResponse;
import com.mark.web.data.entity.from.ShippingAddFrom;
import com.mark.web.data.entity.from.ShippingEditFrom;
import com.mark.web.data.entity.po.Shipping;
import com.mark.web.data.service.IShippingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @Description: 地址模块控制器
 * @Author: Mark
 * @CreateDate: 2018/8/14.
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@RestController
@RequestMapping("/shipping/")
public class ShippingController {
    @Autowired
    private IShippingService iShippingService;
    /**
     *  1.用户添加收货地址
     *
     * @param shippingAddFrom ：新增收货地址的表单
     * @param bindingResult：验证结果
     * @return
     */
    @PostMapping(value = "add.do")
    public ServerResponse add(@Valid ShippingAddFrom shippingAddFrom, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return ServerResponse.createByErrorMessage(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        Shipping shipping = new Shipping();
        BeanUtils.copyProperties(shippingAddFrom,shipping);
        return iShippingService.addShipping(CurrentUser.getCurrentUserId(), shipping);
    }

    /**
     * 2.用户删除收货地址
     * @param shippingId：收货地址id
     * @return
     */
    @PostMapping(value = "delete.do")
    public ServerResponse delete(Integer shippingId){
        return iShippingService.deleteShipping(CurrentUser.getCurrentUserId(), shippingId);
    }

    /**
     * 3.用户编辑收货地址
     * @param shippingEditFrom：编辑收货地址的表单
     * @param bindingResult：验证的结果
     * @return
     */
    @PostMapping(value = "update.do")
    public ServerResponse update(@Valid ShippingEditFrom shippingEditFrom, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return ServerResponse.createByErrorMessage(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        Shipping shipping = new Shipping();
        BeanUtils.copyProperties(shippingEditFrom,shipping);
        return iShippingService.updateShipping(CurrentUser.getCurrentUserId(), shipping);
    }

    //4.用户查看他的一项收货地址
    @GetMapping(value = "select.do")
    public ServerResponse select(Integer shippingId) {
        return iShippingService.selectShipping(CurrentUser.getCurrentUserId(), shippingId);
    }

    /**
     * 5.分页查看
     * @param pageNum：页数
     * @param pageSize：页面大小
     * @return
     */
    @PostMapping(value = "list.do")
    public ServerResponse list(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        return iShippingService.listShipping(CurrentUser.getCurrentUserId(),pageNum,pageSize);
    }
}
